package com.mapzone.showmap;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
//import android.widget.TextView;

public class MainActivity extends Activity {

//	private TextView msg_text;
	private List<String> items = null;  
	private List<String> paths = null; 
	private String rootPath = "/sdcard"; 
	private String curPath = "/sdcard";  
	private AlertDialog.Builder builder ;
	
	private Button bt_open_map;

	float x, y;
	boolean b_db_click = false;
	float first_click = 0;
	float second_click = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		bt_open_map = ( Button )findViewById( R.id.bt_open_map);
		bt_open_map.setOnClickListener( new MyClickListener( ) );
		
	}

	public class MyClickListener implements OnClickListener
	{
		public static final int FILE_RESULT_CODE = 1;
		public MyClickListener(  )
		{
			super();
		}
		public void onClick( View v )
		{
			if( bt_open_map  == v )
			{
//				Intent intent = new Intent();
//				intent.setClass( MainActivity.this, MapActivityNew.class );
//				startActivity( intent );
				getFileDir(rootPath); 
				builder	= new AlertDialog.Builder( MainActivity.this );
				showBuilder( builder );
			}
		}
	}

	private void showBuilder( AlertDialog.Builder builder )
	{
		builder.setTitle(R.string.choice_map_doc )
		.setCancelable( false )
		.setItems( items.toArray( new String[items.size()]), new ItemClick() )
		.setPositiveButton( R.string.yes, new DialogInterface.OnClickListener() 
		{
			public void onClick( DialogInterface dialog, int id )
			{
//				msg_text.setText( "Yes on click." );
			}
		})
		.setPositiveButton( R.string.cancel, new DialogInterface.OnClickListener()
		{
			public void onClick( DialogInterface dialog, int id )
			{
//				msg_text.setText( "Cancel on click" );
				dialog.cancel();
			}
		})
		.show();
	}

	public class ItemClick implements DialogInterface.OnClickListener
	{
		public void onClick( DialogInterface dialog, int item )
		{
			File file = new File(paths.get(item));  
			if (file.isDirectory()) {  
				curPath = paths.get(item);  
				getFileDir(paths.get(item));  
				showBuilder( builder );
			} else {
				curPath = paths.get(item);
				
				Intent intent = new Intent( MainActivity.this, MapActivityNew.class );
//				Intent intent = new Intent( MainActivity.this, MapSurfaceActivity.class );
				intent.putExtra( "path", curPath );
				startActivity( intent );
//				openMapDoc( curPath );
				//可以打开文件  
			}  
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

	private void getFileDir(String filePath) {  
		items = new ArrayList<String>();  
		paths = new ArrayList<String>();  
		File f = new File(filePath);  
		File[] files = f.listFiles();  
		if (!filePath.equals(rootPath)) {  
			items.add( (String) getText( R.string.goto_root ) );  
			paths.add(rootPath);  
			items.add( (String) getText( R.string.goto_parent ) );  
			paths.add(f.getParent());  
		}  
		for (int i = 0; i < files.length; i++) {
			File file = files[i];  
			if( !file.isDirectory() )
			{
				if( !file.getName().endsWith( ".zti" ) && !file.getName().endsWith( ".zmp" ) ) 
				{
					continue;
				}
			}
			else	if( file.getName().indexOf( "mapzone" ) == -1 )
			{
				continue;
			}
			items.add(file.getName()); 
			paths.add(file.getPath());  
		}
		//		setListAdapter( new FileAdapter(this, items, paths ) ); 
	} 

//	@Override
//	public boolean onTouchEvent(MotionEvent event) {
////	       float x = event.getX();
////	        float y = event.getY();
////	        switch(event.getAction())
////	        {
////	        case MotionEvent.ACTION_DOWN:
////	        		second_click = System.currentTimeMillis();
////	        		if( second_click - first_click < 1000 )
////	        		{
//////	        			ndk.on_touch_double_click( 0, x, y );
////	        			msg_text.setText( "on touch dbclick" );
////	        		}
////	        		else
////	        		{
////	        			first_click = second_click;
//////	        			ndk.on_touch_down( 0, x, y );
////	        			msg_text.setText( "on touch down" );
////	        		}
////	            break;
////	        case MotionEvent.ACTION_MOVE:
//////	        	ndk.on_touch_move( 0, x, y );
////	            break;
////	        case MotionEvent.ACTION_UP:
//////	        	ndk.on_touch_up( 0, x, y );
////	            break;
////	        }
////	        mv.postInvalidate();
//	        return super.onTouchEvent(event);
//	}

}
